#ifndef KORLAPADMIN_MODEL_H
#define KORLAPADMIN_MODEL_H

#ifdef __QT5_TARGET

#include <QtSql>
#include <QtSql/QSqlDatabase>
#include <QtCore/QString>
#include <QtCore/QVariant>
#include <QtCore/QLocale>
#include <QtCore/QList>
#include <QtCore/QPair>

#else

#include <QtSql>
#include <QSqlDatabase>
#include <QString>
#include <QVariant>
#include <QLocale>
#include <QList>
#include <QPair>

#endif

#include "korlapadmin_model_global.h"
#include "Exception.h"
#include "PatientManager.h"
#include "CouldNotConnectToDataBaseException.h"
#include "CouldNotExecQueryException.h"
#include "CouldNotCastException.h"
#include "NullPointerException.h"
#include "FilterManager.h"
#include "VisitManager.h"
#include "Schedule.h"
#include "ScheduleManager.h"
#include "Utils.hpp"
#include "QueryBuilder.h"

class KORLAPADMIN_MODEL_EXPORT korlapAdmin_model
{
public:
	korlapAdmin_model();
	~korlapAdmin_model();
	
	int init();
	int connectToDB(const QString& hostName="localhost", const QString& SID="korhaz",const QString& user="zalan", const QString& password="zalan");
	
	QList<int> addPatient(unsigned int fileno,const QString& name, const QString& phone, QDate date);
	void addSchedule(int file_nr,QDateTime* startDate, QDateTime* endDate, const QString& note);
	void updateSchedule(Schedule* s, const QString& note);
	void deleteSchedule(Schedule* s);
	void deleteSchedules(QList<Schedule*>);
	void loadFilters();
	PatientManager* getPatientManager();
	FilterManager* getFilterManager();
	VisitManager* getVisitManager();
	const Filter* getDefaultFilter();
	void loadCurrentLocale();
	QLocale* getCurrentLocale();
	QPair<QDateTime*,QDateTime*>* calculateDaysOfThisWeek();
	QPair<QDateTime*,QDateTime*>* calculateDaysOfTheWeek(QDateTime* dt);
	void buildTimeTable(QDateTime* startDate);
	QList<Schedule*> getSchedulesForThisWeek();
	QMap<QDateTime*,Schedule*>* getScheduleMapForThisWeek();
	QMap<QDateTime*,Schedule*>* getScheduleMap(QPair<QDateTime*,QDateTime*>* dates);
	QList<Schedule*> getSchedules(QPair<QDateTime*,QDateTime*>* dates);
	Schedule* getSchedule(QDateTime* time);
	QDateTime*** getCurrentTimeTable();
	QDateTime*** getTimeTable(QDateTime* dt);

private:
	QueryBuilder qb;
	bool isLoaded;
	void _addTimeTable();
	void _initializeTimeTable();
	QDateTime*** currentTimeTable;
	QDateTime*** thisWeekTable;	
	QMap<QDateTime,QDateTime***>* timeTableMap;
	QPair<QDateTime*,QDateTime*>* weekRange;
	PatientManager* pm;
	FilterManager* fm;
	VisitManager* vm;
	ScheduleManager* sm;
	QSqlDatabase * db;
	Exception e;
	QLocale* locale;
};

#endif // KORLAPADMIN_MODEL_H
